package com.source.openoo;

import java.util.HashMap;

import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XCloseable;


public class OOoStreamConverter {
	
    private XComponentContext xComponentContext;

    public OOoStreamConverter(XComponentContext xComponentContext) {
        this.xComponentContext = xComponentContext;
    }

    public void convert(OOoInputStream input, OOoOutputStream output, String filterName, HashMap<String, String> hm) throws Exception {
    	
        XMultiComponentFactory xMultiComponentFactory = xComponentContext.getServiceManager();
        Object desktopService = xMultiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", xComponentContext);
        XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopService);
        
        PropertyValue[] conversionProperties = new PropertyValue[2];
        conversionProperties[0] = new PropertyValue();
        conversionProperties[1] = new PropertyValue();

        conversionProperties[0].Name = "InputStream";
        conversionProperties[0].Value = input;
        conversionProperties[1].Name = "Hidden";
        conversionProperties[1].Value = new Boolean(true);

        XComponent document = xComponentLoader.loadComponentFromURL("private:stream", "_blank", 0, conversionProperties);

//        if (document != null) {
//            // Retrieve Fields
//            XTextFieldsSupplier fieldSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
//            //XStyleFamiliesSupplier styleFamiliesSupplier = (XStyleFamiliesSupplier) UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, document);
//            // View the fields in the document
//            //String[] elementNames = fieldSupplier.getTextFieldMasters().getElementNames();
//            //for (int i = 0; i < elementNames.length; i++) {
//            //    System.out.println("Element No [" + i + "] name = [" + elementNames[i] + "]");
//            //}
//
//            XEnumerationAccess xEnumeratedFields = fieldSupplier.getTextFields();
//            XEnumeration e = xEnumeratedFields.createEnumeration();
//            while (e.hasMoreElements()) {
//                Any tf = (Any) e.nextElement();
//                XTextField xtf = (XTextField) tf.getObject();
//
//                // get the properties of the field
//                XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xtf);
//                //Property[] arrprop = xPropertySet.getPropertySetInfo().getProperties();
////                try {
////                    // for ms-word mailmergefield the fieldcode looks
////                    // like: MERGEFIELD  FIELD_3  * MERGEFORMAT
////                    String fieldName = (String) xPropertySet.getPropertyValue("VariableName");
////
////                    // Check if the field exists in the hashmap.
////
////                    if (hm.containsKey(fieldName)) {
////                        String value = (String) hm.get(fieldName);
////
////                        // set the content of the field
////                        xPropertySet.setPropertyValue("Content", value);
////                    }
////
////                } catch (UnknownPropertyException ex) {
////                    // Fieldcode property doesn't exist, ignore
////                    System.out.println("[===] Field property does not exist - ignore");
////                }
//            }        
//        }
        
        conversionProperties[0].Name = "OutputStream";
        conversionProperties[0].Value = output;
        conversionProperties[1].Name = "FilterName";
        conversionProperties[1].Value = filterName;

        XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class,document);
        xstorable.storeToURL("private:stream", conversionProperties);

        XCloseable xclosable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class,document);
        xclosable.close(true);
    }

}